package com.zxx.pattern.commandPattern.command3;

import android.graphics.Canvas;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 绘制请求封装类
 *
 * @author zxx on 2021/3/8
 */
public class DrawInvoker {

    //绘制列表
    private List<IDrawPath> drawList = Collections.synchronizedList(new ArrayList<IDrawPath>());

    //重做列表
    private List<IDrawPath> redoList = Collections.synchronizedList(new ArrayList<IDrawPath>());

    //增加一个命令
    public void add(IDrawPath command) {
        redoList.clear();
        drawList.add(command);
    }

    //撤销上一步的操作
    public void undo() {
        if (drawList.size() > 0) {
            IDrawPath undo = drawList.get(drawList.size() - 1);
            drawList.remove(drawList.size() - 1);
            undo.undo();

            redoList.add(undo);
        }
    }

    //重做上一步撤销的命令
    public void redo() {
        if (redoList.size() > 0) {
            IDrawPath redoCommand = redoList.get(redoList.size() - 1);
            redoList.remove(redoList.size() - 1);
            drawList.add(redoCommand);
        }
    }


    //执行命令
    public void execute(Canvas canvas) {
        if (drawList != null) {
            for (IDrawPath tmp : drawList) {
                tmp.draw(canvas);
            }
        }
    }

    //是否可以重做
    public boolean canRedo(){
        return redoList.size() > 0;
    }


    //是否可以撤销
    public boolean canUndo(){
        return drawList.size() > 0;
    }


}
